import axios from 'axios'
import router from '../router'
import { Message } from 'element-ui'

// axios.defaults.withCredentials = true

const service = axios.create({
    timeout: 5000,
    // baseURL: 'proxy',
    headers: {
        'Content-Type': 'application/json'
    }
})

service.interceptors.request.use(
    config => {
        return config
    },
    err => {
        return Promise.reject(err)
    }
)

service.interceptors.response.use(
    config => {
        if (config.data.code === -1 && config.data.msg === 'NOTLOGIN') {
            if (JSON.parse(localStorage.getItem('userinfo'))) {
                localStorage.removeItem('userinfo')
                Message.warning('未登录或登录状态失效，请重新登录...(一秒后跳转至登录页)')
                setTimeout(() => {
                    router.push('/login')
                }, 1000)
            } else {
                router.push('/login')
            }
        } else {
            return config
        }
    },
    err => {
        return Promise.reject(err)
    }
)

export default service